//
//  YKAddContactViewController.m
//  WeChat
//
//  Created by 吴高烽 on 2016/11/9.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKAddContactViewController.h"
//#import "XMPPJID.h"
@interface YKAddContactViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textfield;

@end

@implementation YKAddContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addContactAction:(id)sender {
    NSString  *user =  self.textfield.text ;
    
//    不能添加自己为好友
    if ([user isEqualToString:[YKAccount shareAccount].loginUser]) {
        [self showAlert:@"不能添加自己为好友"];
        return ;
            }
//    不能添加已经是好友的为好友
    XMPPJID *userjid = [XMPPJID jidWithUser:user domain:[YKAccount shareAccount].domain resource:nil];
    BOOL userExists = [[YKXMPPTool shareYKXMPPTool].rosterStorage userExistsWithJID:userjid xmppStream:[YKXMPPTool shareYKXMPPTool].xmppStream];
    if (userExists) {
        [self showAlert:@"用户已存在"];
        return ;
    }
//   订阅好友
    [[YKXMPPTool shareYKXMPPTool].roster subscribePresenceToUser:userjid];
/*    添加不存在的好友，也能显示在通讯录中 解决方法:
 一、服务器可以拦截添加好友的请求，如果当前数据库没有好友就不要返回信息。
 二、过滤数据库的Subscribe查询请求
    none 对方没有同意添加好友
    to 发给对方的请求
    from 别人发来的请求
    both 双方互为好友
  */
    
}
-(void)showAlert:(NSString*)msg{
    UIAlertController *aControll = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [aControll addAction:action];
    [self presentViewController:aControll animated:YES completion:nil];

}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
